package com.Sql;

import java.sql.SQLException;
import java.util.*;

import org.apache.log4j.Logger;

import com.ibatis.sqlmap.client.SqlMapClient;

public class MSDao
{
  private boolean AutoCommint = true;
  private SqlMapClient sqlMap = null;
  public static MSDao instance = null;

  private static Logger log = Logger.getLogger(MSDao.class);

  public static MSDao getInstance()
  {
    if (instance == null) {
      instance = new MSDao();
    }

    return instance;
  }

  public static MSDao getInstance(boolean AutoCommint)
  {
    if (instance == null) {
      instance = new MSDao(AutoCommint);
    }

    return instance;
  }

  public MSDao()
  {
    try
    {
      this.sqlMap = DatabaseFactory.getInstance().getMSSqlMapClient();
    } catch (Exception e) {
      log.error("sqlMap ERROR - " + e.getMessage());
    }
  }

  public MSDao(boolean AutoCommint)
  {
    this.AutoCommint = AutoCommint;
    try
    {
      this.sqlMap = DatabaseFactory.getInstance().getMSSqlMapClient();

      if (!this.AutoCommint)
        this.sqlMap.startTransaction();
    }
    catch (Exception e) {
      log.error("sqlMap ERROR - " + e.getMessage());
    }
  }

  public void endTransaction()
  {
    try
    {
      if (!this.AutoCommint) {
        this.sqlMap.commitTransaction();
        this.sqlMap.endTransaction();
      }
    } catch (Exception e) {
      log.error("sqlMap close ERROR - " + e.getMessage());
    }
  }

  public Map<String, Object> getView(String ns, Map<String, Object> param)
    throws SQLException
  {
    Map result = new HashMap();
    result = (Map)this.sqlMap.queryForObject(ns, param);
    return result;
  }

  public Map<String, Object> getView(String ns)
    throws SQLException
  {
    Map result = new HashMap();
    result = (Map)this.sqlMap.queryForObject(ns);
    return result;
  }

  public List<Map<String, Object>> getList(String ns, Map<String, Object> param)
    throws SQLException
  {
    List list = new ArrayList();
    list = this.sqlMap.queryForList(ns, param);
    return list;
  }

  public List<Map<String, Object>> getList(String ns)
    throws SQLException
  {
    List list = new ArrayList();
    list = this.sqlMap.queryForList(ns);
    return list;
  }

  public Object getInsert(String ns, Map<String, Object> param)
    throws SQLException
  {
    Object obj = null;
    obj = this.sqlMap.insert(ns, param);
    return obj;
  }

  public int getUpdate(String ns, Map<String, Object> param)
    throws SQLException
  {
    int obj = 0;
    obj = this.sqlMap.update(ns, param);
    return obj;
  }

  public int getDelete(String ns, Map<String, Object> param)
    throws SQLException
  {
    int obj = 0;
    obj = this.sqlMap.delete(ns, param);
    return obj;
  }

  public Object getObject(String ns, Map<String, Object> param)
    throws SQLException
  {
    Object obj = null;
    obj = this.sqlMap.queryForObject(ns, param);

    return obj;
  }

  public Object getObject(String ns)
    throws SQLException
  {
    Object obj = null;
    obj = this.sqlMap.queryForObject(ns);

    return obj;
  }

  public String getString(String ns, Map<String, Object> param)
    throws SQLException
  {
    String obj = "";
    obj = this.sqlMap.queryForObject(ns, param).toString();

    return obj;
  }

  public String getString(String ns)
    throws SQLException
  {
    String obj = null;
    obj = this.sqlMap.queryForObject(ns).toString();

    return obj;
  }

  public int getInt(String ns, Map<String, Object> param)
    throws SQLException
  {
    int obj = 0;
    obj = ((Integer)this.sqlMap.queryForObject(ns, param)).intValue();

    return obj;
  }

  public int getInt(String ns)
    throws SQLException
  {
    int obj = 0;
    obj = ((Integer)this.sqlMap.queryForObject(ns)).intValue();

    return obj;
  }
}